
# coding: utf-8

# In[1]:

from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
import numpy as np


# In[2]:

walk_times = 5000
number_of_steps = 1000
each_step = np.random.randint(0, 2, size=(walk_times, number_of_steps))
each_step = np.where(each_step > 0, 1, -1)
walk_path = each_step.cumsum(1)


# In[3]:

# 求解离出发点最远的距离
max_distance = np.abs(walk_path).max()


# In[4]:

# 求解各次模拟中走出的最远距离的平均值
average_max_distance = np.abs(walk_path).max(axis=1).mean()


# In[5]:

# 求解最终位置的平均距离
destination_distance = np.abs(walk_path[:, -1]).mean()


# In[6]:

#求解第一次达到距原点10距离的平均时间
time_to_10 = (np.abs(walk_path >= 10)).argmax(axis=1).mean()


# In[14]:

#求解穿过出发点的平均次数，此处即统计0在行走路径的记录数组中出现了多少次
times_to_origin = np.where(walk_path == 0, 1, 0).sum(axis = 1).mean()


# In[15]:

#求解改变方向的平均次数，此处统计在单步记录的数组中有多少个数字与其前一位刚好是相反数(1和-1)
change_count = []
for i in range(len(each_step)):
    single_count = 0
    for j in range(1, len(each_step[i])):
        if each_step[i][j] != each_step[i][j-1]:
            single_count += 1
        change_count.append(single_count)
change_count = np.array(change_count)
change_count_average = change_count.mean()


# In[16]:

#求解5000次模拟中有多少次模拟的路径有超过30的记录
hit_30 = (np.abs(walk_path) >= 30).any(1)
hit_30_count = hit_30.sum()


# In[17]:

plt.figure(1)
# font = FontProperties(fname="./SimHei.ttc", size=10)
plt.plot(np.arange(1, 1001, dtype=int), walk_path[0], color='red', linestyle='-', label="walk_path")
plt.legend(loc='upper left')
plt.xlabel(u'步数')
plt.ylabel(u'距离')
plt.show()


# In[20]:

# 输出统计信息
print("醉汉走出的最远距离:", max_distance)
print("醉汉走出的最远距离平均值:", average_max_distance)
print("醉汉的最终位置平均距出发点距离:", destination_distance)
print("醉汉第一次到达距离出发点为10处花费的时间单位平均值:", time_to_10)
print("醉汉平均经过出发点的次数:", times_to_origin)
print("醉汉平均改变方向的次数:", change_count_average)
print("醉汉在%d次模拟中到达了距离出发点大于等于30处." % hit_30_count)


# In[ ]:



